<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @expand-change="expandChange"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot-scope="scope" slot="status">
        <el-tag :type="scope.row.status==0?'info':''">{{scope.row.$status}}</el-tag>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button @click="toDeliver(scope.row.id)" icon="el-icon-edit" v-if="scope.row.status==2" type="text" size="small">填写运单号</el-button>
        <el-button @click="salesReturnShow(scope.row.id)" icon="el-icon-edit" v-if="scope.row.status==10||scope.row.status==11" type="text" size="small">售后处理</el-button>
        <el-button @click="refundShow(scope.row.id)" icon="el-icon-edit" v-if="scope.row.status==12" type="text" size="small">售后处理</el-button>
      </template>
      <template slot="expand" slot-scope="{row}">
        <div style="margin-top: -30px">
          <div v-for="i in row.itemsData" >
            <el-row :gutter="10" style="margin-top: 30px">
              <el-col :push="1" span="20" style="background: linear-gradient(45deg, rgb(255, 255, 255), rgb(255 253 253), transparent);border-radius: 10px;box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;margin-top: -10px;padding-top: 10px">
                <el-col :span="2" style="height: 100px">
                  <div style="box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;padding-top: 10px">
                    <el-image  :fit="'scale-down'" style="height: 70px;width: 100%;"  :src="i.skuImage"></el-image>
                  </div>
                </el-col>
                <el-col :span="4">
                  <el-tag class="orderItems">
                    {{i.spuTitle}}
                  </el-tag>
                </el-col>
                <el-col :span="4" >
                  <el-tag class="orderItems">{{i.attrValue}}</el-tag>
                </el-col>
                <el-col :span="2">
                  <el-tag class="orderItems">数量:{{i.skuCount}}</el-tag>
                </el-col>
                <el-col :span="2">
                  <el-tag class="orderItems">原价:{{i.skuPrice}}/件</el-tag>
                </el-col>
                <el-col :span="2">
                  <el-tag  class="orderItems">邮费(合计):{{i.spuPostage}}</el-tag>
                </el-col>
                <el-col :span="2">
                  <el-tag  class="orderItems">结算:{{i.finalPrice}}/件</el-tag>
                </el-col>
              </el-col>
            </el-row>
          </div>
        </div>
      </template>
      <template slot="menuLeft">
        <el-tag>其余状态请使用上方搜索</el-tag>
        <el-tabs v-model="activeName" @tab-click="handleTabsClick">
          <el-tab-pane label="待发货" name='2'></el-tab-pane>
          <el-tab-pane label="退货退款" name='11'></el-tab-pane>
          <el-tab-pane label="仅退款" name='12'></el-tab-pane>
        </el-tabs>
      </template>
    </avue-crud>


    <el-dialog title="售后处理-仅退款"
               :visible.sync="refundBoxShow"
               width="60%">
      <el-tag style="margin-top: -20px;margin-bottom: 20px" type="warning">无论是否同意售后,商家备注都将向用户显示</el-tag>
      <avue-form style="width: 100%" ref="refund" v-model="refundForm" :option="refundOptions"  @submit="refundSubmit">
        <template slot-scope="scope" slot="menuForm">
          <el-button v-if="refundForm.status==1" @click="refundSubmit(refundForm)">退款</el-button>
          <el-button v-if="refundForm.status==1" @click="refuseOrder(refundForm.id,refundForm.shopRemark)">拒绝</el-button>
        </template>
      </avue-form>


    </el-dialog>
    <el-dialog title="售后处理-退货退款"
               :visible.sync="salesReturnBoxShow"
               width="60%">
      <el-tag style="margin-top: -20px;margin-bottom: 20px" type="warning">无论是否同意售后,商家备注都将向用户显示</el-tag>
      <avue-form @submit="" style="width: 100%" ref="salesReturn" v-model="salesReturnForm" :option="salesReturnOptions">
        <template slot-scope="scope" slot="menuForm">
          <el-button v-if="salesReturnForm.status==1"  type="primary" @click="waitSendOrder(salesReturnForm)">同意退货</el-button>
          <el-button type="primary" v-if="salesReturnForm.status==3" @click="refundSubmit(salesReturnForm)">退 款</el-button>
          <el-button   v-if="salesReturnForm.status==1||salesReturnForm.status==3" @click="refuseOrder(salesReturnForm.id,salesReturnForm.shopRemark)">拒绝</el-button>
          <el-tooltip  v-if="salesReturnForm.status==2" class="item" effect="dark" content="买家填写运单号后,您才可以进行下一步操作" placement="top-start">
             <el-button type="info" >等待买家寄回</el-button>
          </el-tooltip>


        </template>
      </avue-form>


    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove,deliver} from "@/api/order/order";
  import {orderAfterDetail,refund,refuse,waitSend} from "@/api/order/orderafter";
  import {getOrderItems} from "@/api/order/orderdetail";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        salesReturnBoxShow:false,
        refundBoxShow:false,
        refundForm:{

        },
        salesReturnForm:{

        },
        salesReturnOptions:{
          printBtn:true,
          submitBtn:false,
          excelBtn:true,
          emptyBtn:false,
          column:[
            {
              label:'售后类型',
              dicUrl: "/api/ocm-system/dict/dictionary?code=after_order_type",
              type:"select",
              width:120,
              span: 12,
              disabled:true,
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              prop: "type",
              rules: [{
                required: true,
                message: "请输入姓名",
                trigger: "blur"
              }]
            },
            {
              label:'售后原因',
              dicUrl: "/api/ocm-system/dict/dictionary?code=after_order_reason",
              type:"select",
              width:120,
              span: 12,
              disabled:true,
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              prop: "reason",
              rules: [{
                required: true,
                message: "请输入姓名",
                trigger: "blur"
              }]
            },{
              label:'退款金额',

              type:"input",
              width:120,
              span: 12,
              disabled:true,
              prop: "amount",
              rules: [{
                required: true,
                message: "请输入姓名",
                trigger: "blur"
              }]
            },{
              label:'买家备注',
              span: 24,
              type: 'textarea',
              width:120,
              disabled:true,
              prop: "remark",
              rules: [{
                required: false,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label:'当前状态',
              dicUrl: "/api/ocm-system/dict/dictionary?code=after_order_status",
              type:"select",
              width:120,
              span: 12,
              disabled:true,
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              prop: "status",
              rules: [{
                required: true,
                message: "请输入姓名",
                trigger: "blur"
              }]
            },
            {
              label:'物流单号',
              type:"input",
              width:120,
              span: 12,
              display:false,
              disabled:true,
              prop: "logistics",
              control:(val,form)=>{
                if(form.status!==1){
                  return {
                    logistics:{
                      display:form.status===3||form.status===10
                    },
                    address:{
                      disabled:true
                    },
                    consignee:{
                      disabled:true
                    },
                    consigneePhone: {
                      disabled:true
                    },
                    shopRemark:{
                      disabled:true
                    }
                  }
                }else{
                  return {
                    logistics:{
                      display:form.status===3||form.status===10
                    },
                    address:{
                      disabled:false
                    },
                    consignee:{
                      disabled:false
                    },
                    consigneePhone: {
                      disabled:false
                    },
                    shopRemark:{
                      disabled:false
                    }
                  }
                }
              },
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label:'退货地址',
              span: 24,
              type: 'textarea',
              width:120,
              disabled: false,
              prop: 'address',
              rules: [{
                required: true,
                message: "请输入姓名",
                trigger: "blur"
              }]
            },
            {
              label:'收货人',
              type:"input",
              width:120,
              disabled: false,
              span: 12,
              prop: "consignee",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label:'手机号',
              type:"input",
              disabled: false,
              width:120,
              span: 12,
              prop: "consigneePhone",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label:'商家备注',
              span: 24,
              type: 'textarea',
              disabled: false,
              width:120,
              prop: 'shopRemark',
              rules: [{
                required: false,
                message: "请输入姓名",
                trigger: "blur"
              }]
            },
          ]},
        refundOptions:{
          submitBtn:false,
          emptyBtn:false,
          printBtn:true,
          column:[
            {
              label:'售后类型',
              dicUrl: "/api/ocm-system/dict/dictionary?code=after_order_type",
              type:"select",
              width:120,
              span: 12,
              disabled:true,
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              prop: "type",
              rules: [{
                required: true,
                message: "请输入姓名",
                trigger: "blur"
              }]
            },
            {
              label:'售后原因',
              dicUrl: "/api/ocm-system/dict/dictionary?code=after_order_reason",
              type:"select",
              width:120,
              span: 12,
              disabled:true,
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              prop: "reason",
              rules: [{
                required: true,
                message: "请输入姓名",
                trigger: "blur"
              }]
            },{
              label:'买家备注',
              span: 24,
              type: 'textarea',
              width:120,
              disabled:true,
              prop: "remark",
              rules: [{
                required: false,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label:'当前状态',
              dicUrl: "/api/ocm-system/dict/dictionary?code=after_order_status",
              type:"select",
              width:120,
              span: 12,
              disabled:true,
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              prop: "status",
              rules: [{
                required: true,
                message: "请输入姓名",
                trigger: "blur"
              }]
            },{
              label:'商家备注',
              span: 24,
              type: 'textarea',
              width:120,
              disabled:true,
              prop: "shopRemark",
              control:(val,form)=>{
                if(form.status==1){
                  return {
                    shopRemark:{
                      disabled:false
                    }
                  }
                }
              },
              rules: [{
                required: false,
                message: "请输入",
                trigger: "blur"
              }]
            }
         ]},
        activeName:'2',
        form: {},
        query: {
          status:2
        },
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {    excelBtn:true,
          expand: true,
          height: 'auto',
          rowKey:'id',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          index: true,
          delBtn:false,
          viewBtn: true,
          addBtn:false,
          selection: false,
          column: [
            {
              label: "原价(RMB)",
              hide:false,
              editDisplay: false,
              prop: "initialPrice",
              rules: [{
                required: true,
                message: "请输入初始价格",
                trigger: "blur"
              }]
            },
            {
              label: "减免",
              hide:true,
              prop: "deratePrice",
              rules: [{
                required: true,
                message: "请输入减免价格",
                trigger: "blur"
              }]
            },
            {
              label: "结算(RMB)",
              prop: "finalPrice",
              rules: [{
                required: true,
                message: "请输入最终价格",
                trigger: "blur"
              }]
            },
            {
              label: "收货省份",
              prop: "province",
              rules: [{
                required: true,
                message: "请输入收货省份",
                trigger: "blur"
              }]
            },
            {
              label: "收货市区",
              prop: "city",
              rules: [{
                required: true,
                message: "请输入收货市区",
                trigger: "blur"
              }]
            },
            {
              label: "收货区",
              prop: "district",
              rules: [{
                required: true,
                message: "请输入收货区",
                trigger: "blur"
              }]
            },
            {
              label: "收货手机号",
              prop: "phone",
              rules: [{
                required: true,
                message: "请输入收货手机号",
                trigger: "blur"
              }]
            },
            {
              label: "收货详细地址",
              prop: "address",
              rules: [{
                required: true,
                message: "请输入收货详细地址",
                trigger: "blur"
              }]
            },
            {
              label: "收货人",
              prop: "consignee",
              rules: [{
                required: true,
                message: "请输入收货人",
                trigger: "blur"
              }]
            },
            {
              label: "物流单号",
              hide:true,
              editDisplay:false,
              prop: "logistics",
              rules: [{
                required: true,
                message: "请输入物流单号",
                trigger: "blur"
              }]
            },
            {
              label: "客服备注",
              prop: "serviceRemark",
              rules: [{
                required: true,
                message: "请输入客服备注",
                trigger: "blur"
              }]
            },
            {
              label: "订单备注",
              prop: "orderRemark",
              editDisplay:false,
              rules: [{
                required: true,
                message: "请输入订单备注",
                trigger: "blur"
              }]
            },
            {
              label: "邮费",
              hide:true,
              prop: "orderPostage",
              editDisplay:false,
              rules: [{
                required: true,
                message: "请输入邮费",
                trigger: "blur"
              }]
            },
            {
              label: "店铺id",
              editDisplay:false,
              hide:true,
              prop: "shopId",
              rules: [{
                required: true,
                message: "请输入店铺id",
                trigger: "blur"
              }]
            },
            {
              label: "状态",
              type:"select",
              width:120,
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              search:true,
              dicUrl: "/api/ocm-system/dict/dictionary?code=order_status",
              editDisplay:false,
              prop: "status",
              rules: [{
                required: true,
                message: "请输入支付时间",
                trigger: "blur"
              }]
            },
            {
              label: "支付时间",
              hide:true,
              editDisplay:false,
              prop: "payTime",
              rules: [{
                required: true,
                message: "请输入支付时间",
                trigger: "blur"
              }]
            },
            {
              label: "完成时间",
              hide:true,
              editDisplay:false,
              prop: "finalTime",
              rules: [{
                required: true,
                message: "请输入完成时间",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.order_add, false),
          viewBtn: this.vaildData(this.permission.order_view, false),
          delBtn: this.vaildData(this.permission.order_delete, false),
          editBtn: this.vaildData(this.permission.order_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      waitSendOrder(row){
        this.$refs['salesReturn'].validate((valid) => {
          if (valid) {
            waitSend({
              id:row.id,
              address:row.address,
              consigneePhone:row.consigneePhone,
              consignee:row.consignee,
              shopRemark:row.shopRemark
            }).then(res=>{
              this.salesReturnBoxShow=false
              this.$refs['salesReturn'].allDisabled=false

          },error => {
              this.salesReturnBoxShow=false
              this.$refs['salesReturn'].allDisabled=false
          })
        }});

      },
      refuseOrder(id,shopRemark){
        refuse({id:id,shopRemark:shopRemark}).then(res=>{
          this.refundBoxShow=false
          this.salesReturnBoxShow=false
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        })
      },
      refundSubmit(row){
        refund(row.id).then(res=>{

          this.refundBoxShow=false
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {

        })

      },
      refundShow(id){
        orderAfterDetail(id).then(res=>{
          this.refundForm=res.data.data
        })
        this.refundBoxShow=true
      },
      salesReturnShow(id){
        orderAfterDetail(id).then(res=>{
          console.log(res.data.data)
          this.salesReturnForm=res.data.data
        })
        this.salesReturnBoxShow=true
      },
      handleTabsClick(v){
        this.query.status=Number(this.activeName)
        this.onLoad(this.page)
      },
      toDeliver(id){

       this.$prompt('请输入单号', '提示', {
         confirmButtonText: '确定',
         cancelButtonText: '取消',
       }).then(({ value }) => {
         deliver({id:id,logistics:value}).then(res=>{
           this.onLoad(this.page);
           this.$message({
             type: "success",
             message: "操作成功!"
           });
         })
       })
      },
      async expandChange(row, expendList) {
        let res=await getOrderItems({orderId: row.id})
        this.$set(row,'itemsData',res.data.data)
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
  .orderItems{
      overflow: hidden;
      margin-top:10px;
      text-overflow:ellipsis;
      white-space: initial !important;
      width: 100%;
      height: 70px !important;
  }
</style>
